package org.example.selector;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * 客户端
 */
public class Demo1Client {
    public static void main(String[] args) throws InterruptedException {
        //创建socket通道
        try (SocketChannel socketChannel = SocketChannel.open()) {
            //设置非阻塞
            socketChannel.configureBlocking(false);
            //连接
            if (!socketChannel.connect(new InetSocketAddress("127.0.0.1", 10040))) {
                while (!socketChannel.finishConnect()) System.out.println("连接中...");
            }
            //创建缓冲区
            ByteBuffer buffer = ByteBuffer.wrap("66666".getBytes());
            //写入通道
            socketChannel.write(buffer);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
